var parentId="0";
var txthospitalId='';
$(function(){
	var opType=$("#txtfrmUser").val();
if(opType=="edit")
	{
	
	//province  =${locationId};
	
	//locationId=${hospital.locationId};
	//alert(locationId);
	}
	//构建角色列表
txthospitalId=$("#txthospitalId").val();
if(txthospitalId==''){
	txthospitalId='0';
	
}


$("#gridUser").datagrid({
    width: "100%",
    height: 'auto',
    url: "../hospitalDepartment/listData",
    method: 'get',
    sortName: 'title',
    loadMsg: "数据加载中...",
    pagination:true,
    pageSize: 9,//每页显示的记录条数，默认为10
    pageList:[9,10,20],//每页显示几条记录
    striped: true,
    singleSelect:true,
    toolbar:"#tbUser"
});
$("#gridUser").datagrid("load",{
	random:function(){return Math.random();},
	hospitalId:txthospitalId

});

listLocation();
createSelCity();
//修改

});


 

//条件查询
function search()
{
	var hospitalAddName=$("#txtHospitalAddName").val();
	$('#gridUser').datagrid('load',{
		  name:hospitalAddName
	});
}
/**
 * 构建操作列
 * val: the field value，也就是field:'id'。
 * row: the row record data。就是这一行的Json数据，包括你已经选择在Datagrid上显示的内容，和没显示的内容。
 * index:索引号
 */
function optColumn(val,row,index)
{
	
	var optStr="<a href='javascript:void(0)' onclick='javascript:delUser("+val+",\""+row.name+"\")' style='color:blue'><u>删除</u></a>";
	optStr+="&nbsp;&nbsp;";
	optStr+="<a href='javascript:void(0)' onclick='javascript:dlgUserOpenEdit("+val+","+index+")' style='color:blue'><u>编辑</u></a>";
	return optStr;
}

/**
 * 删除用户
 * @param userId 用户ID
 * @param userName 用户名
 */
function delUser(userId,userName)
{
	$.messager.confirm("提示", "是否删除用户【"+userName+"】？", function(r){

		if (r){
			$.ajax({
				type: "get",
				url: "../hospitalDepartment/delete",
				data:{
					id:userId
				},
				success: function(data){
					if(data.result==true)
					{
						txthospitalId=$("#txthospitalId").val();
						$("#gridUser").datagrid("load",{
							random:function(){return Math.random();},
						    hospitalId:txthospitalId
						});
					}
					else
		            {
		            	$.messager.alert("提示",data.msg,"error");
		            }
				},
				error:function(err){
					$.messager.alert("提示",err.responseText,null,function(){
						window.location.href="../login/toLogin";
					});
				}
			});
		}
	});
}


//清空表单
function clearUserFrom()
{

	$("#frmDepartment").form('clear');
}

//打开人员编辑对话框
function dlgUserOpen()
{
	$("#dlgUser").window('open');
}

//打开新增用户对话框
function dlgUserOpenAdd()
{     
if($("#txthospitalId").val()==''){
	$.messager.alert("提示","请先添加医院","error");
	
}else{
	//jsFrmUser();
	$("#txtOpType").val("add");
	clearUserFrom();
	
	$("#texthospitalId").val($("#txthospitalId").val());
	dlgUserOpen();
  }
}

//取消人员编辑
function dlgUserCancel()
{
	$("#dlgUser").window('close');
	clearUserFrom();
}

//新增用户提交
function addUserSubmit()
{
	$('#frmDepartment').form('submit',{  
        url:'../hospitalDepartment/save',  
        onSubmit:function(){
            return $(this).form('validate');
        },  
        success:function(jsonstr){
        	var data=JSON.parse(jsonstr); 
            if(data.result==true)
            {
            	//提交成功处理
            	//dlgUserCancel();
            	
            	$('#frmDepartment').form('clear');
            	
            	$("#texthospitalId").val($("#txthospitalId").val());
            	txthospitalId=$("#txthospitalId").val();
            	$("#gridUser").datagrid("load",{
            		random:function(){return Math.random();},
            		hospitalId:txthospitalId
            	});
            	$.messager.alert("提示",data.msg);
            }
            else
            {
            	$.messager.alert("提示",data.msg,"error");
            }
        }
    }); 
}

//修改用户提交
function editUserSubmit()	
{
	$('#frmDepartment').form('submit',{  
        url:'../hospitalDepartment/edit',  
        onSubmit:function(){
            return $(this).form('validate');
        },  
        success:function(jsonstr){
        	var data=JSON.parse(jsonstr); 
            if(data.result==true)
            {
            	//提交成功处理
            	dlgUserCancel();
            	txthospitalId=$("#txthospitalId").val();
            	$("#gridUser").datagrid("load",{
            		random:function(){return Math.random();},
            		hospitalId:txthospitalId
            	
            	});
            	$.messager.alert("提示",data.msg);
            }
            else
            {
            	$.messager.alert("提示",data.msg,"error");
            }
        }
    }); 
}

/**
 * 打开修改用户对话框
 * @param userId:用户ID
 * @param rowIndex:表格索引号
 */
function dlgUserOpenEdit(id,rowIndex)
{
	clearUserFrom();
	$('#gridUser').datagrid('selectRow',rowIndex);
	var hdtment=$('#gridUser').datagrid('getSelected');	
	$("#HospitalDepartmentId").val(hdtment.id);
	$("#texthospitalId").val(hdtment.hospitalId);
	$("#textcreateDate").val(hdtment.createDate);
	$("#textname").textbox("setValue",hdtment.name);
	//.val(hdtment.name);
	$("#textdescription").val(hdtment.description);
   //获取科室id 以及医院id
	$("#txtOpType").val("edit");
	
	dlgUserOpen();
}

//提交表单
function submitForm()
{   
	var opType=$("#txtOpType").val();
	//新增
	if(opType=="add")
	{
		addUserSubmit();
	}
	
	//修改
	if(opType=="edit")
	{
		editUserSubmit();
	}
}
//*****************************************以下为医院表单提交和科室表单提交******************************************
//医院表单提交
function jsFrmUser()
{ 
	var opType=$("#txtfrmUser").val();
	//新增
	if(opType=="add")
	{   
		addhSubmit();
	}
	
	//修改
	if(opType=="edit")
	{
		edithSubmit();
	}
}
//表单提交
function addhSubmit(){
	if($("#province").val()==''){
		$("#province").focus();
		$.messager.alert("提示","省份不能为空","error");
		return false;
	}
	if($("#locationId").val()==''){
		$("#locationId").focus();
		$.messager.alert("提示","城市不能为空","error");
		return false;
	}
$('#frmUser').form('submit',{
    url:'../hospital/save', 
    onSubmit:function(){
        return $(this).form('validate');
    }, 
    success:function(jsonstr){
    	var data=JSON.parse(jsonstr); 
    	var hospitalId=data.id
        	//提交成功处理
    	$("#txthospitalId").val(hospitalId);
    	$("#hospitalId").val(hospitalId);
    	$("#txtfrmUser").val("edit");
        $.messager.alert("提示",data.msg);
    }
}); 
}
function edithSubmit(){
	if($("#province").val()==''){
		$("#province").focus();
		$.messager.alert("提示","省份不能为空","error");
		return false;
	}
	if($("#locationId").val()==''){
		$("#locationId").focus();
		$.messager.alert("提示","城市不能为空","error");
		return false;
	}
	$('#frmUser').form('submit',{
		 url:'../hospital/edit', 
		 onSubmit:function(){
		     return $(this).form('validate');
	     }, 
	     success:function(jsonstr){
	     	var data=JSON.parse(jsonstr); 
	        if(data.result==true)
	        {
        	//提交成功处理
        	$.messager.alert("提示",data.msg);
	        }
	        else
	        {
	        	$.messager.alert("提示",data.msg,"error");
	        }
	     }
	});
}

//获取地区列表
function listLocation(){
	var opType=$("#txtfrmUser").val();
	//修改	
	$.ajax({
        async : false,  
        cache : true,
        type : 'post',  
        url : '../location/grade/next',
		data:{
			'parentId':parentId
		},
        success:function(data){
        	
        	var locationStr="<option value=''>请选择省份</option>";
        	for(var i=0;i<data.length;i++)
        	{ 
        		var location=data[i];
        		if(province==location.id){
        			locationStr+="<option value='"+location.id+"' selected=selected>"+location.name+"</option>";
        		}else{
        		locationStr+="<option value='"+location.id+"'>"+location.name+"</option>";
        		}
        	}
	  $("#province").html(locationStr);

        }
	})
	
}

function createSelCity(){
	
	 parentId=$("#province").val();
	$.ajax({
        async : false,  
        cache : true,
        type : 'post',  
        url : '../location/grade/next',
		data:{
			'parentId':parentId
		},
        success:function(data){
        	var locationStr="<option value=''>请选择城市</option>";
        	for(var i=0;i<data.length;i++)
        	{ 
        		var location=data[i];
        		if(locationId==location.id){
        			
        			locationStr+="<option value='"+location.id+"' selected=selected>"+location.name+"</option>";
        		}else{
        		locationStr+="<option value='"+location.id+"'>"+location.name+"</option>";
        		}
        	}
	  $("#locationId").html(locationStr);

        }
	})
}
